package com.person.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
  * 16.最接近的三数之和
  * 2022-05-20 15:26:55
**/
public class Sequence_0016_ThreeSumClosest {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<>();
        Object o = new Object();
        arr.add("a");
        List<String> collect = arr.stream().map(e -> e + "123").collect(Collectors.toList());
        collect.forEach(e-> System.out.println(e));
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int len = nums.length;
        int ans = 100000000;
        // a < b < c
        int a = 0, b, c;
        for (; a < len - 2; a++) {
            if (a > 0 && nums[a] == nums[a - 1]) {
                continue;
            }
            for (b = a + 1; b < len - 1; b++) {
                if (b - 1 > a && nums[b] == nums[b - 1]) {
                    continue;
                }
                Object o = new Object();
                c = len - 1;
                for (; c > b; c--) {
                    int cand = nums[a] + nums[b] + nums[c];
                    if (cand - target == 0){
                        return target;
                    }else if (cand -target > 0){
                        if(Math.abs(ans-target) > Math.abs(cand-target)){
                            ans = cand;
                        }
                    }else{
                        if(Math.abs(ans-target) > Math.abs(cand-target)){
                            ans = cand;
                        }
                        break;
                    }
                }

            }
        }
        return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
